﻿using System.Collections.Generic;
using System.Configuration;
using System.Web;

namespace Wedn.Core.Utility
{
    public abstract class Config : ConfigHelper
    {
        //private static Dictionary<string, string> templateRule = null;
        //private static Dictionary<string, string> siteConfig = null;
        public static Dictionary<string, string> TemplateRule
        {
            get
            {
                if (HttpRuntime.Cache["Wedn_TemplateRule"] == null)
                {
                    HttpRuntime.Cache["Wedn_TemplateRule"] = LoadTemplateRule();
                }
                return HttpRuntime.Cache["Wedn_TemplateRule"] as Dictionary<string, string>;
            }
        }
        public static Dictionary<string, string> SiteConfig
        {
            get
            {
                if (HttpRuntime.Cache["Wedn_SiteConfig"] == null)
                {
                    HttpRuntime.Cache["Wedn_SiteConfig"] = LoadConfig();
                }
                return HttpRuntime.Cache["Wedn_SiteConfig"] as Dictionary<string, string>;
            }
        }
        private static string siteRoot;
        /// <summary>
        /// 程序相对根路径
        /// </summary>
        public static string SiteRoot
        {
            get
            {
                if (string.IsNullOrEmpty(siteRoot))
                {
                    siteRoot = ConfigurationManager.AppSettings["SiteRoot"];
                }
                return siteRoot;
            }
        }

        private static string siteprefix;
        /// <summary>
        /// Cache、Session、Cookies 等应用程序变量名前缀
        /// </summary>
        public static string SitePrefix
        {
            get
            {
                if (string.IsNullOrEmpty(siteprefix))
                {
                    siteprefix = SiteConfig["SitePrefix"];
                }
                return siteprefix;
            }
        }

        private static string encryptkey;
        /// <summary>
        /// Cache、Session、Cookies 等应用程序变量名前缀
        /// </summary>
        public static string EncryptKey
        {
            get
            {
                if (string.IsNullOrEmpty(encryptkey))
                {
                    encryptkey = SiteConfig["EncryptKey"];
                }
                return encryptkey;
            }
        }

        /// <summary>
        /// 程序Url
        /// </summary>
        public static string SiteUrl
        {
            get
            {
                string siteurl = "http://" + System.Web.HttpContext.Current.Request.Url.Host + SiteRoot;
                if (System.Web.HttpContext.Current.Request.Url.Port != 80)
                {
                    siteurl = "http://" + System.Web.HttpContext.Current.Request.Url.Host + ":" + System.Web.HttpContext.Current.Request.Url.Port + SiteRoot;
                }
                return siteurl;
            }
        }

        private static string sqlconnectionstring;
        /// <summary>
        /// SqlServer 数据库连接字符串
        /// </summary>
        public static string SqlConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(sqlconnectionstring))
                {
                    sqlconnectionstring = SiteConfig["SqlConnectionString"];
                }
                return sqlconnectionstring;
            }
        }

        private static string oledbconnectionstring;
        /// <summary>
        /// Access 数据库连接字符串
        /// </summary>
        public static string OleDbConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(oledbconnectionstring))
                {
                    oledbconnectionstring = SiteConfig["OleDbConnectionString"];
                }
                return oledbconnectionstring;
            }
        }

        private static string tableprefix;
        /// <summary>
        /// 数据库表前缀
        /// </summary>
        public static string TablePrefix
        {
            get
            {
                if (string.IsNullOrEmpty(tableprefix))
                {
                    tableprefix = SiteConfig["TablePrefix"];
                }
                return tableprefix;
            }
        }

        private static string dalpath;
        /// <summary>
        /// 数据库操作类组件名
        /// </summary>
        public static string DALPath
        {
            get
            {
                if (string.IsNullOrEmpty(dalpath))
                {
                    dalpath = SiteConfig["DALPath"];
                }
                return dalpath;
            }
        }

        private static string dalnamespace;
        /// <summary>
        /// 数据库操作类命名空间
        /// </summary>
        public static string DALNameSpace
        {
            get
            {
                if (string.IsNullOrEmpty(dalnamespace))
                {
                    dalnamespace = SiteConfig["DALNameSpace"];
                }
                return dalnamespace;
            }
        }
    }
}
